/*
 * Package il.ac.biu.cs.grossmm.api.data
 * File PublicationPoint.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 */

package il.ac.biu.cs.grossmm.api.flow;

import java.util.Enumeration;

import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.Node;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.keys.Key;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;

/**
 * PublictaionPoint provides means to creating, deleting, inspecting and
 * manipulating of data nodes of given type with a given key node pattern.
 */
public interface PublicationPoint<N> {
	Root<N> createNode(Key key, boolean mayExist)
		throws OperationFailedException, NodeExistsException;

	/**
	 * Get subscribed data node by key. The key should refer to a subscribed
	 * node. Otherwise the method returns <tt>null</tt> even if node exists.
	 * 
	 * @param key
	 *            the key
	 * @return subscribed data node
	 * @throws OperationFailedException
	 */
	Root<N> getRoot(Key key) throws OperationFailedException;

	/**
	 * Deletes root data node. The object is deleted only after all locks are
	 * released. New locks cannot be obtained after this method returns.
	 * 
	 * @param key
	 *            the key which identifies the node
	 * @throws OperationFailedException
	 */
	void deleteNode(Key key) throws OperationFailedException;

	/**
	 * Locks the subtree rooted at given node for writing and returns node
	 * manipulator (<tt>NodeManipulator</tt> object). If this node (or a
	 * subtree to which it belongs) has been deleted then null is returned.
	 * <p>
	 * This operation locks at least the subtree rooted at given node, but may
	 * lock more nodes (e.g. the entire tree) depending on the implementation
	 * 
	 * 
	 * 
	 * @param node
	 *            the node to lock for writing
	 * @return the node manipulator, null is returned if node has been deleted
	 * @throws OperationFailedException
	 */
	NodeManipulator writeLock(Node node) throws OperationFailedException;

	/**
	 * Releases the lock associated with the subtree rooted at given node.
	 * 
	 * @param node
	 *            the node to unlock for writing
	 */
	void writeUnlock(Node node);

	/**
	 * Queries whether the point is persistent (i.e. data not lost when server
	 * shuts down)
	 * 
	 * @return true iff this publication point is persistent
	 * 
	 * 
	 */
	boolean isPersistent();

	/**
	 * Queries for all keys for which there are published nodes on this
	 * publication point
	 * 
	 * @return enumeration of all keys on this publciation point
	 */
	Enumeration<Key> getKeys();

	/**
	 * Queries for all keys which match a specified pattern and for which there
	 * are published nodes on this publication point
	 * 
	 * @param pattern
	 *            the key pattern to restrict the returned enumeration of keys
	 * 
	 * @return enumeration of all keys on this publciation point
	 */
	Enumeration<Key> getKeys(KeyPattern pattern);
}
